<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class NewsModel extends Model
{
    //
    public static function get($id = null)
    {
        if ($id == null) {
            $lists = DB::table('news')->orderBy('created_at', 'desc')->get();
            return $lists;
        } else {
            $id = (int)$id;
            $lists = DB::table('news')->where('id', $id)->get();
            return $lists;
        }
    }

    public static function delete_id($id)
    {
        if (DB::table('news')->where('id', $id)->exists()) {
            DB::table('news')->where('id', $id)->delete();
            return true;
        } else {
            return false;
        }
    }

    public static function update_new($request_array)
    {
        if (DB::table('news')->where('id', $request_array['id'])->exists()) {
            DB::table('news')->where('id', $request_array['id'])->update([
                'show' => $request_array['show'],
                'title' => $request_array['title'],
                'micro' => $request_array['micro'],
                'content' => $request_array['content']
            ]);
            return true;
        } else {
            return false;
        }
    }

    public static function add($request_array)
    {
        DB::table('news')->insert([
            'id' => $request_array['id'],
            'show' => $request_array['show'],
            'title' => $request_array['title'],
            'micro' => $request_array['micro'],
            'content' => $request_array['content'],
            'created_at' => date('Y-m-d h:i:s'),
            'created_id' => Auth::id()
        ]);
        return true;
    }
}
